<?php

function d4($i)
{
    if ($i < 10)
    {
        return "000" . $i;
    }
    if ($i < 100)
    {
        return "00" . $i;
    }
    if ($i < 1000)
    {
        return "0" . $i;
    }
    return $i;
}

if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
$ui = new UI();
$conf = new Config();
include( "admin_menu.php" );
$tpl = new Template2();
$editok = 1;
if ($curruser['account'] != G_SUPERACCOUNT)
{
    $rs = getdb("select * from lotto where open='" . date("Y-m-d") . "'");
    if (!$rs->eof && 0 < $rs->f['n1'] + $rs->f['n2'] + $rs->f['n3'] + $rs->f['n4'] + $rs->f['n5'] + $rs->f['n6'] + $rs->f['spec'])
    {
        $editok = 0;
    }
}
if (ispost())
{
    if ($editok == 1 && $conf->get("calcbet") == 0)
    {
        $stopbet = $conf->get("stopbet");
        if (getvar("stopbet") != "" && $stopbet != getvar("stopbet"))
        {
            if (getvar("stopbet") < $stopbet)
            {
                if ($stopbet < $conf->get("linebet"))
                {
                    $conf->set("linebet", $stopbet);
                }
                if ($stopbet < $conf->get("waitbet"))
                {
                    $conf->set("waitbet", $stopbet);
                }
                if ($stopbet < $conf->get("othebet"))
                {
                    $conf->set("othebet", $stopbet);
                }
            }
            $stopbet = getvar("stopbet");
            $conf->set("stopbet", $stopbet);
            $sql = "update k_user set stopbet='" . $stopbet . "'";
            getdb($sql);
        }
        if (getvar("linebet") != "" && getvar("linebet") <= $stopbet)
        {
            $conf->set("linebet", d4(intval(getvar("linebet"))));
        }
        if (getvar("waitbet") != "" && getvar("waitbet") <= $stopbet)
        {
            $conf->set("waitbet", d4(intval(getvar("waitbet"))));
        }
        if (getvar("othebet") != "" && getvar("othebet") <= $stopbet)
        {
            $conf->set("othebet", d4(intval(getvar("othebet"))));
        }
        if (getvar("startbet") != "")
        {
            $conf->set("startbet", d4(intval(getvar("startbet"))));
        }
    }
    if ($curruser['acl'] == 255)
    {
        if (isset($_COOKIE['g_language']) && $_COOKIE['g_language'] == "zh-cn")
        {
            include_once( "class/ccharset.php" );
            $cc = new CCharset();
            $conf->set("news", safeHTML(getvar("message")));
            $conf->set("news_en", safeHTML(getvar("message_en")));
            $conf->set("news_th", safeHTML(getvar("message_th")));
            $conf->set("news_pm", safeHTML(getvar("message_pm")));
        }
        else
        {
            $conf->set("news", safeHTML(getvar("message")));
            $conf->set("news_en", safeHTML(getvar("message_en")));
            $conf->set("news_th", safeHTML(getvar("message_th")));
            $conf->set("news_pm", safeHTML(getvar("message_pm")));
        }
        if (intval($_POST['chinayear']) != 0)
        {
            $conf->set("chinayear", intval($_POST['chinayear']));
        }
    }
    if (getvar("systemcheck_all") != "")
    {
        $conf->set("systemcheck", intval(getvar("systemcheck_all")));
    }
    $conf->set("systemcheck_6h", intval($_POST['systemcheck']));
    if (getvar("ipshow") != "")
    {
        $conf->set("ipshow", intval(getvar("ipshow")));
    }
    $conf->set("calcbet", intval($_POST['calcbet']));
    $conf->set("protect_start", intval($_POST['sortstar']));
    $qchkbx = isset($_POST['check_6s_all']);
    $conf->set("check_6s_all", $qchkbx);
    $qchkbx = isset($_POST['autoport_mode']);
    $conf->set("autoport_mod", $qchkbx);
    $qchkbx = isset($_POST['edit_back_mode']);
    $conf->set("edit_back_mode", $qchkbx);
    if (getvar("betmin") != "")
    {
        $conf->set("betmin", intval(getvar("betmin")));
    }
    $conf->set("betmax", 0 - 1);
    if (getvar("minbackbet") != "")
    {
        $conf->set("minbackbet", intval($_POST['minbackbet']));
    }
    if (getvar("max_all_percent") != "" && intval(getvar("max_all_percent")) <= 100)
    {
        $conf->set("max_all_percent", intval(getvar("max_all_percent")));
    }
    if (getvar("edit_percent_wod") != "")
    {
        $conf->set("edit_percent_wod", intval($_POST['edit_percent_wod']));
    }
    if (getvar("show_paper_day") != "")
    {
        $conf->set("show_paper_day", intval($_POST['show_paper_day']));
    }
    $page_count_ag = intval(getvar("page_count_ag"));
    $page_count_ag = $page_count_ag == 11 ? 90 : $page_count_ag;
    if ($page_count_ag != 0)
    {
        $conf->set("page_max_count_ag", $page_count_ag);
    }
    if (getvar("AdjustAmountMode") != "")
    {
        $conf->set("AdjustAmountMode", intval($_POST['AdjustAmountMode']));
    }
    if (getvar("rate0a_b") != "")
    {
        $conf->set("rate0a_b", floatval($_POST['rate0a_b']));
    }
    if (getvar("rate0a_c") != "")
    {
        $conf->set("rate0a_c", floatval($_POST['rate0a_c']));
    }
    if (getvar("rate0a_d") != "")
    {
        $conf->set("rate0a_d", floatval($_POST['rate0a_d']));
    }
    if (getvar("rate1a_b") != "")
    {
        $conf->set("rate1a_b", floatval($_POST['rate1a_b']));
    }
    if (getvar("rate1a_c") != "")
    {
        $conf->set("rate1a_c", floatval($_POST['rate1a_c']));
    }
    if (getvar("rate1a_d") != "")
    {
        $conf->set("rate1a_d", floatval($_POST['rate1a_d']));
    }
    if (getvar("line_count") != "")
    {
        $conf->set("line_max_count", intval($_POST['line_count']));
    }
    if (getvar("page_count") != "")
    {
        if (intval($_POST['page_count']) == 11)
        {
            $conf->set("page_max_count", 20);
        }
        else
        {
            $conf->set("page_max_count", intval($_POST['page_count']));
        }
    }
    if (getvar("snowopen") != "")
    {
        $conf->set("snow_open", intval($_POST['snowopen']));
    }
    if (getvar("rate0open") != "")
    {
        $conf->set("reat0_open", intval($_POST['rate0open']));
    }
    if (getvar("editportmod") != "")
    {
        $conf->set("edit_autoprot_mod", intval($_POST['editportmod']));
    }
    if (getvar("autoratelinema") != "" && 0 <= floatval($_POST['autoratelinema']) && floatval($_POST['autoratelinema']) <= 1)
    {
        $conf->set("autorate_linema", floatval($_POST['autoratelinema']));
    }
    if (getvar("maxrate0") != "" && 0 <= floatval($_POST['maxrate0']) && floatval($_POST['maxrate0']) <= 3)
    {
        $conf->set("max_rate0", floatval($_POST['maxrate0']));
    }
    $bet21_27 = isset($_POST['bettype21_27']);
    $conf->set("bettype21_27", $bet21_27);
}
$tpl->set("action", "?" . $_SERVER['QUERY_STRING']);
$member_limit = intval($conf->Get("member_limit"));
if ($editok == 0)
{
    $tpl->set("edit_exc", "<tr><td colspan='2'><font color='red'>注意，已经颁奖无法变更时间！</font></td></tr>");
}
$tpl->set("linebet", $conf->Get("linebet"));
$tpl->set("waitbet", $conf->Get("waitbet"));
$tpl->set("startbet", $conf->Get("startbet"));
$tpl->set("stopbet", $conf->Get("stopbet"));
$tpl->set("othebet", $conf->Get("othebet"));
$tpl->set("betmin", $conf->Get("betmin"));
$tpl->set("betmax", $conf->Get("betmax"));
$tpl->set("max_all_percent", $conf->Get("max_all_percent"));
$tpl->set("autoratelinema", $conf->get("autorate_linema"));
$tpl->set("maxrate0", $conf->Get("max_rate0"));
$tpl->set("message", $conf->Get("news"));
$tpl->set("message_en", $conf->Get("news_en"));
$tpl->set("message_th", $conf->Get("news_th"));
$tpl->set("message_pm", $conf->Get("news_pm"));
$years = "";
$membermaxlimit = "";
$y = $conf->get("chinayear");
if ($curruser['account'] == G_SUPERACCOUNT && $curruser['acl'] == 255)
{
    $membermaxlimit = "&#12288;※ 本月份最大连线人数：<b>" . intval($conf->get("member_max_limit")) . "</b>";
    $i = 1;
    while ($i <= 12)
    {
        $years .= "<option value=" . $i;
        if ($i == $y)
        {
            $years .= " selected";
        }
        $years .= ">" . $g_chinayear[$i] . "</option>";
        ++$i;
    }
}
$years .= "<option value=" . $y . " selected>";
$years .= $g_chinayear[$y] . "</option>";
$tpl->set("years", $years);
$systemcheck = intval($conf->get("systemcheck_6h"));
$tpl->set("systemcheck" . $systemcheck, " checked");
$tpl->set("systemcheck_CHK" . intval($conf->Get("systemcheck")), " checked ");
$tpl->set("ipshow_CHK" . intval($conf->Get("ipshow")), " checked ");
$calcbet = intval($conf->get("calcbet"));
$tpl->set("calcbet" . $calcbet, " checked");
$sortstar = intval($conf->get("protect_start"));
$tpl->set("sortstar" . $sortstar, " checked");
$check_6s_alls = intval($conf->get("check_6s_all"));
if ($check_6s_alls == 1)
{
    $tpl->set("check6sall", " checked");
}
$autoport_mode = intval($conf->get("autoport_mod"));
if ($autoport_mode == 1)
{
    $tpl->set("autoportmod", " checked");
}
$edit_back_mode = intval($conf->get("edit_back_mode"));
if ($edit_back_mode == 1)
{
    $tpl->set("editbackmode", " checked");
}
$weekallname = array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "不限制", "不开放");
$editwods = "";
$y = $conf->get("edit_percent_wod");
$slcShow_paper_day = "";
$show_paper_day = $conf->get("show_paper_day");
$i = 0;
while ($i <= 8)
{
    $editwods .= "<option value='" . $i . "'";
    if ($i == $y)
    {
        $editwods .= " selected";
    }
    $editwods .= ">" . $weekallname[$i] . "</option>";
    $slcTemp = $i == $show_paper_day ? " selected" : "";
    $slcShow_paper_day .= "<option value='{$i}' {$slcTemp}>{$weekallname[$i]}</option>";
    ++$i;
}
$tpl->set("editwods", $editwods);
$tpl->set("minbackbet", $conf->get("minbackbet"));
$tpl->set("show_paper_day", $slcShow_paper_day);
$tpl->set("rate0a_b", $conf->get("rate0a_b"));
$tpl->set("rate0a_c", $conf->get("rate0a_c"));
$tpl->set("rate0a_d", $conf->get("rate0a_d"));
$tpl->set("rate1a_b", $conf->get("rate1a_b"));
$tpl->set("rate1a_c", $conf->get("rate1a_c"));
$tpl->set("rate1a_d", $conf->get("rate1a_d"));
$weekallname = array("10", "11", "12", "13", "14");
$editwods = "";
$y = intval($conf->get("line_max_count"));
$i = 0;
while ($i <= 4)
{
    $editwods .= "<option value='" . ( $i + 10 ) . "'";
    if ($i + 10 == $y)
    {
        $editwods .= " selected";
    }
    $editwods .= ">" . $weekallname[$i] . "</option>";
    ++$i;
}
$tpl->set("linecount", $editwods);
$AdjustAmountMode = intval($conf->get("AdjustAmountMode"));
$adjamtArr = array("不调整", "百分比", "全归零");
$adjamtOpts = "";
$i = 0;
while ($i <= 2)
{
    $slcTemp = $i == $AdjustAmountMode ? " selected" : "";
    $adjamtOpts .= "<option value='{$i}' {$slcTemp}>{$adjamtArr[$i]}</option>";
    ++$i;
}
$tpl->set("AdjustAmountMode", $adjamtOpts);
$weekallname = array("", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "全部");
$editwods = "";
$y = min(11, intval($conf->get("page_max_count")));
$y2 = min(11, intval($conf->get("page_max_count_ag")));
$page_max_count_ag = "";
$i = 1;
while ($i <= 11)
{
    $editwods .= "<option value='" . $i . "'";
    if ($i == $y)
    {
        $editwods .= " selected";
    }
    $editwods .= ">" . $weekallname[$i] . "</option>";
    $slcTemp = $i == $y2 ? " selected" : "";
    $page_max_count_ag .= "<option value='{$i}' {$slcTemp}>{$weekallname[$i]}</option>";
    ++$i;
}
$tpl->set("pagecount", $editwods);
$tpl->set("page_count_ag", $page_max_count_ag);
$today = date("Y-m-d");
$today = date("Y-m-d");
$minDate = getPaperMinDate(0 - 1, 1);
$page_count_date_str = "区间:{$minDate}～{$today}";
$minDate = getPaperMinDate(0 - 1);
$page_count_ag_date_str = "区间:{$minDate}～{$today}";
$tpl->set(array("page_count_date_str" => $page_count_date_str, "page_count_ag_date_str" => $page_count_ag_date_str));
$snowopen = intval($conf->get("snow_open"));
$tpl->set("snowopen" . $snowopen, " checked");
$rate0open = intval($conf->get("reat0_open"));
$tpl->set("rate0open" . $rate0open, " checked");
$editportmod = intval($conf->get("edit_autoprot_mod"));
$tpl->set("editportmod" . $editportmod, " checked");
$bettype21_27 = intval($conf->get("bettype21_27"));
$tpl->set("bettype21_27", $bettype21_27 == 1 ? "checked" : "" );
$tpl->set("member_num", $member_limit);
$tpl->set("max_member_num", $membermaxlimit);
$tpl->load("main", "admin.htm");
$tpl->show("main");
?>